package ru.sanluck.tg.command;

import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.bots.AbsSender;
import ru.sanluck.tg.Dialog;
import ru.sanluck.tg.TgUser;
import ru.sanluck.tg.TgUserService;
import ru.sanluck.tg.UserMapper;

import java.util.Optional;
import java.util.Set;

@Service
public class Start extends AbsTgCommand {
    private static final String START = "/start";
    private static final String START_MESSAGE = "Добро пожаловать в библиотеку, %s!";

    public Start(TgUserService tgUserService, UserMapper userMapper) {
        super(tgUserService, userMapper);
    }

    @Override
    public String commandText() {
        return START;
    }

    @Override
    public Set<Dialog> dialogs() {
        return Set.of(Dialog.START);
    }

    @Override
    public void executeCommand(Message message, AbsSender sender) {
        User user = message.getFrom();
        TgUser tgUser = Optional
                .ofNullable(tgUserService.getUser(user.getUserName()))
                .orElse(createUser(user, Dialog.START));
        tgUserService.changeDialog(user.getUserName(), Dialog.START);
        sendTextMessage(message.getChatId(), String.format(START_MESSAGE, tgUser.getUserName()), sender);
    }
}
